/*
 * MotelPlusView.java
 */

package motelplus;

import forms.frmViewApts;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import rad.Java.MotelPlus.Entries.frmRegisterProduct;
import rad.Java.MotelPlus.Entries.frmRegisterApt;
import rad.Java.MotelPlus.Entries.dlgRegisterConfiguration;
import rad.Java.MotelPlus.Entries.frmRegisterAptType;
import rad.Java.MotelPlus.Entries.frmRegisterBlock;
import rad.Java.MotelPlus.Entries.frmRegisterEmployee;
import rad.Java.MotelPlus.Views.frmViewMostUsedRooms;
import rad.Java.MotelPlus.Views.frmViewMovement;

/**
 * The application's main frame.
 */
public class MotelPlusView extends FrameView {

    public MotelPlusView(SingleFrameApplication app) {
        super(app);

        initComponents();

        load();
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });


    }

    private void load()
    {

    }
    
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = MotelPlusApp.getApplication().getMainFrame();
            aboutBox = new MotelPlusAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        MotelPlusApp.getApplication().show(aboutBox);
    }

    @Action
    public void showRegisterAptType()
    {
        new frmRegisterAptType().setVisible(true);
    }

    @Action
    public void showRegisterBlock()
    {
        new frmRegisterBlock().setVisible(true);
    }

    @Action
    public void showMoviment()
    {
        new frmViewMovement().setVisible(true);
    }

    @Action
    public void showReportsRoom()
    {
        new frmViewMostUsedRooms("Renato").setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu mniType = new javax.swing.JMenu();
        mniRegisterProducts = new javax.swing.JMenuItem();
        mniRegisterApts = new javax.swing.JMenuItem();
        mniRegisterAptsTypes = new javax.swing.JMenuItem();
        mniRegisterBlocks = new javax.swing.JMenuItem();
        mniRegisterEmployee = new javax.swing.JMenuItem();
        mniSettings = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        mnuStay = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 249, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(motelplus.MotelPlusApp.class).getContext().getResourceMap(MotelPlusView.class);
        mniType.setText(resourceMap.getString("mniType.text")); // NOI18N
        mniType.setName("mniType"); // NOI18N
        mniType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniTypeActionPerformed(evt);
            }
        });

        mniRegisterProducts.setText(resourceMap.getString("mniRegisterProducts.text")); // NOI18N
        mniRegisterProducts.setName("mniRegisterProducts"); // NOI18N
        mniRegisterProducts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniRegisterProductsActionPerformed(evt);
            }
        });
        mniType.add(mniRegisterProducts);

        mniRegisterApts.setText(resourceMap.getString("mniRegisterApts.text")); // NOI18N
        mniRegisterApts.setName("mniRegisterApts"); // NOI18N
        mniRegisterApts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniRegisterAptsActionPerformed(evt);
            }
        });
        mniType.add(mniRegisterApts);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(motelplus.MotelPlusApp.class).getContext().getActionMap(MotelPlusView.class, this);
        mniRegisterAptsTypes.setAction(actionMap.get("showRegisterAptType")); // NOI18N
        mniRegisterAptsTypes.setText(resourceMap.getString("mniRegisterAptsTypes.text")); // NOI18N
        mniRegisterAptsTypes.setName("mniRegisterAptsTypes"); // NOI18N
        mniType.add(mniRegisterAptsTypes);

        mniRegisterBlocks.setAction(actionMap.get("showRegisterBlock")); // NOI18N
        mniRegisterBlocks.setText(resourceMap.getString("mniRegisterBlocks.text")); // NOI18N
        mniRegisterBlocks.setName("mniRegisterBlocks"); // NOI18N
        mniType.add(mniRegisterBlocks);

        mniRegisterEmployee.setText(resourceMap.getString("mniRegisterEmployee.text")); // NOI18N
        mniRegisterEmployee.setName("mniRegisterEmployee"); // NOI18N
        mniRegisterEmployee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniRegisterEmployeeActionPerformed(evt);
            }
        });
        mniType.add(mniRegisterEmployee);

        mniSettings.setText(resourceMap.getString("mniSettings.text")); // NOI18N
        mniSettings.setName("mniSettings"); // NOI18N
        mniSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniSettingsActionPerformed(evt);
            }
        });
        mniType.add(mniSettings);

        jSeparator1.setName("jSeparator1"); // NOI18N
        mniType.add(jSeparator1);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        mniType.add(exitMenuItem);

        menuBar.add(mniType);

        mnuStay.setText(resourceMap.getString("mnuStay.text")); // NOI18N
        mnuStay.setName("mnuStay"); // NOI18N

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        mnuStay.add(jMenuItem1);

        menuBar.add(mnuStay);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem2.setAction(actionMap.get("showMoviment")); // NOI18N
        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenu1.add(jMenuItem2);

        jMenu3.setText(resourceMap.getString("jMenu3.text")); // NOI18N
        jMenu3.setName("jMenu3"); // NOI18N

        jMenuItem3.setAction(actionMap.get("showReportsRoom")); // NOI18N
        jMenuItem3.setText(resourceMap.getString("jMenuItem3.text")); // NOI18N
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        jMenu3.add(jMenuItem3);

        jMenu1.add(jMenu3);

        menuBar.add(jMenu1);

        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N
        menuBar.add(jMenu2);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void mniRegisterProductsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniRegisterProductsActionPerformed
        new frmRegisterProduct().setVisible(true);
    }//GEN-LAST:event_mniRegisterProductsActionPerformed

    private void mniRegisterAptsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniRegisterAptsActionPerformed
        new frmRegisterApt().setVisible(true);
    }//GEN-LAST:event_mniRegisterAptsActionPerformed

    private void mniTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniTypeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mniTypeActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
         new frmViewApts().setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void mniRegisterEmployeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniRegisterEmployeeActionPerformed
        new frmRegisterEmployee().setVisible(true);
    }//GEN-LAST:event_mniRegisterEmployeeActionPerformed

    private void mniSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniSettingsActionPerformed
        new dlgRegisterConfiguration(null, true).setVisible(true);
    }//GEN-LAST:event_mniSettingsActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem mniRegisterApts;
    private javax.swing.JMenuItem mniRegisterAptsTypes;
    private javax.swing.JMenuItem mniRegisterBlocks;
    private javax.swing.JMenuItem mniRegisterEmployee;
    private javax.swing.JMenuItem mniRegisterProducts;
    private javax.swing.JMenuItem mniSettings;
    private javax.swing.JMenu mnuStay;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
